/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.MailTarget;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.cache.NamedEntryCache;
/*     */ import com.zimbra.cs.account.grouphandler.GroupHandler;
/*     */ import com.zimbra.cs.account.ldap.LdapHelper;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExternalGroup
/*     */   extends NamedEntry
/*     */ {
/*  44 */   private static final NamedEntryCache<ExternalGroup> CACHE = new NamedEntryCache(LC.ldap_cache_group_maxsize.intValue(), LC.ldap_cache_group_maxage.intValue() * 60000L);
/*     */   
/*     */ 
/*     */   private final String dn;
/*     */   
/*     */ 
/*     */   private final GroupHandler groupHandler;
/*     */   
/*     */ 
/*     */   private final String zimbraDomainId;
/*     */   
/*     */ 
/*     */ 
/*     */   ExternalGroup(String dn, String id, String name, String zimbraDomainId, ZAttributes attrs, GroupHandler groupHandler, Provisioning prov)
/*     */     throws LdapException
/*     */   {
/*  60 */     super(name, id, attrs.getAttrs(), null, prov);
/*  61 */     this.dn = dn;
/*  62 */     this.groupHandler = groupHandler;
/*  63 */     this.zimbraDomainId = zimbraDomainId;
/*     */   }
/*     */   
/*     */   public String getDN() {
/*  67 */     return this.dn;
/*     */   }
/*     */   
/*     */   public String getZimbraDomainId() {
/*  71 */     return this.zimbraDomainId;
/*     */   }
/*     */   
/*     */   boolean inGroup(MailTarget mailTarget, boolean asAdmin) throws ServiceException {
/*  75 */     if (!(mailTarget instanceof Account)) {
/*  76 */       return false;
/*     */     }
/*  78 */     return inGroup((Account)mailTarget, asAdmin);
/*     */   }
/*     */   
/*     */   boolean inGroup(Account acct, boolean asAdmin) throws ServiceException {
/*  82 */     return this.groupHandler.inDelegatedAdminGroup(this, acct, asAdmin);
/*     */   }
/*     */   
/*     */   private static GroupHandler getGroupHandler(Domain domain) {
/*  86 */     String className = domain.getExternalGroupHandlerClass();
/*  87 */     return GroupHandler.getHandler(className);
/*     */   }
/*     */   
/*     */   private static ExternalGroup makeExternalGroup(Domain domain, GroupHandler groupHandler, String extGroupName, String dn, ZAttributes attrs)
/*     */     throws ServiceException
/*     */   {
/*  93 */     String id = ZimbraACE.ExternalGroupInfo.encode(domain.getId(), extGroupName);
/*  94 */     String name = ZimbraACE.ExternalGroupInfo.encode(domain.getName(), extGroupName);
/*     */     
/*  96 */     ExternalGroup extGroup = new ExternalGroup(dn, id, name, domain.getId(), attrs, groupHandler, LdapProv.getInst());
/*     */     
/*  98 */     return extGroup;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static ExternalGroup get(Key.DomainBy domainBy, String extGroupGrantee, boolean asAdmin)
/*     */     throws ServiceException
/*     */   {
/* 108 */     ExternalGroup group = null;
/*     */     
/* 110 */     if (Key.DomainBy.name == domainBy) {
/* 111 */       group = (ExternalGroup)CACHE.getByName(extGroupGrantee);
/*     */     } else {
/* 113 */       group = (ExternalGroup)CACHE.getById(extGroupGrantee);
/*     */     }
/*     */     
/* 116 */     if (group != null) {
/* 117 */       return group;
/*     */     }
/*     */     
/* 120 */     group = searchGroup(domainBy, extGroupGrantee, asAdmin);
/*     */     
/* 122 */     if (group != null) {
/* 123 */       CACHE.put(group);
/*     */     }
/*     */     
/* 126 */     return group;
/*     */   }
/*     */   
/*     */   private static ExternalGroup searchGroup(Key.DomainBy domainBy, String extGroupGrantee, boolean asAdmin) throws ServiceException
/*     */   {
/* 131 */     LdapProv prov = LdapProv.getInst();
/*     */     
/* 133 */     ZimbraACE.ExternalGroupInfo extGrpInfo = ZimbraACE.ExternalGroupInfo.parse(extGroupGrantee);
/* 134 */     String zimbraDomain = extGrpInfo.getZimbraDmain();
/* 135 */     String extGroupName = extGrpInfo.getExternalGroupName();
/*     */     
/* 137 */     Domain domain = prov.get(domainBy, zimbraDomain);
/* 138 */     if (domain == null) {
/* 139 */       throw AccountServiceException.NO_SUCH_DOMAIN(zimbraDomain);
/*     */     }
/*     */     
/* 142 */     String searchBase = domain.getExternalGroupLdapSearchBase();
/* 143 */     String filterTemplate = domain.getExternalGroupLdapSearchFilter();
/*     */     
/* 145 */     if (searchBase == null) {
/* 146 */       searchBase = "";
/*     */     }
/* 148 */     String searchFilter = LdapUtil.computeDn(extGroupName, filterTemplate);
/*     */     
/* 150 */     GroupHandler groupHandler = getGroupHandler(domain);
/*     */     
/* 152 */     ZLdapContext zlc = null;
/*     */     try {
/* 154 */       zlc = groupHandler.getExternalDelegatedAdminGroupsLdapContext(domain, asAdmin);
/*     */       
/* 156 */       ZSearchResultEntry entry = prov.getHelper().searchForEntry(searchBase, ZLdapFilterFactory.FilterId.EXTERNAL_GROUP, searchFilter, zlc, new String[] { "mail" });
/*     */       
/*     */       ExternalGroup localExternalGroup;
/* 159 */       if (entry != null) {
/* 160 */         return makeExternalGroup(domain, groupHandler, extGroupName, entry.getDN(), entry.getAttributes());
/*     */       }
/*     */       
/* 163 */       return null;
/*     */     }
/*     */     finally {
/* 166 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/ExternalGroup.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */